<?php
/**
 * Created by PhpStorm.
 * User: DaibiDaddy
 * Date: 2016/6/3
 * Time: 14:59
 */

namespace App\Setting\LazyCommon;

use App\Model\LazyCommon\UserModel;
use App\Model\LazyCommon\UserGroupsModel;

class UserSetting{

    public $entityCnName = "用户";//后台管理显示的模块名称
    public $table = "User";//数据库表名
    public $editKeys = array("ban","money");//允许管理列表中直接编辑的字段
    public $perPage = 100;//管理列表 每页显示条数

    //邀请设置
    public $inviteSetKey = "parent";//将邀请人的ID设置到什么字段
    public $inviteGetKey = "id";//邀请码是哪个字段，不特别设置的话就以ID为邀请码
    public $inviteUrlKey = "p";//URL中如何邀请码的KEY

    /*自动获取的配置项*/
    public $entity;
    public $bundleName;
    public $entityName;
    public $SettingPath;
    public $entityPath;
    public $modelPath;
    public $doctrine;

    public function __construct($doctrine)
    {
        $this->bundleName = "App";
        $this->entityName = str_replace("\\","",str_replace("Setting","",str_replace(__NAMESPACE__,"",__CLASS__)));
        $this->SettingPath = __CLASS__;
        $this->entityPath = str_replace($this->entityName."Entity",$this->entityName,str_replace("Setting","Entity",str_replace("\\LazyCommon","",__CLASS__)));
//        $this->entityPath = str_replace($this->entityName."Entity",$this->entityName,str_replace("Setting","Entity",str_replace("\\LazyCommon","",__CLASS__)));
        $this->entity = new $this->entityPath();
        $this->modelPath = str_replace("Setting","Model",__CLASS__);
        $this->doctrine = $doctrine;
    }

    public function listConfig()
    {
        $default = array();
        $default["User"]["select"] = array("id","account","nickName","phone","headPic","addTime","lastLoginTime","lastLoginIp","ban","money","realName");
        $default["User"]["search"] = array("id","nickName","realName","account","phone");
        $default["User"]["condition"] = array("groups");
        $default["User"]["orderBy"] = array("id"=>"DESC");

        $join = array();
        $join["User_Groups AS UserGroups"]["on"] = "User.groups = UserGroups.id";
        $join["User_Groups AS UserGroups"]["select"] = array("name","parent");
        $join["User_Groups AS UserGroups"]["search"] = array();
        $join["User_Groups AS UserGroups"]["condition"] = array();
        $join["User_Groups AS UserGroups"]["orderBy"] = array();

        return array(
            "default"   => $default,
            "joins"     => $join,
        );
    }

    public function formConfig()
    {
        $model = new UserGroupsModel($this->doctrine);
        $categories = $model->getAllUserGroupsTree();
        $parents[0] = "请选择分组";
        foreach ($categories as $c) {
            $parents[$c["id"]] = $c["name"];
        }
        $options["groups"]["other"] = array(
            "choices"   => array_flip($parents),
        );
        $config["groups"] = array(
            "label"     =>"用户组",
            "showType"  =>"select",
            "dataType"  =>"",
            "option"    => $options["groups"]
        );

        $config["realName"] = array(
            "label"     =>"姓名",
            "showType"  =>"text",
            "dataType"  =>"required",
            "option"    => array()
        );

        $config["account"] = array(
            "label"     =>"账号",
            "showType"  =>"text",
            "dataType"  =>"required",
            "option"    => array()
        );

        $config["password"] = array(
            "label"     =>"密码",
            "showType"  =>"text",
            "dataType"  =>"required",
            "option"    => array()
        );
//
//        $config["money"] = array(
//            "label"     =>"薪酬标准",
//            "showType"  =>"text",
//            "dataType"  =>"required",
//            "option"    => array()
//        );

//        $config["passwordPay"] = array(
//            "label"     =>"安全密码",
//            "showType"  =>"text",
//            "dataType"  =>"required",
//            "option"    => array()
//        );

        return $config;
    }
}